package com.csii.adhoc.dataunits.service;

import com.csii.adhoc.dataready.TDataCondPo;
import com.csii.adhoc.dataready.TDataCondVo;
import com.csii.adhoc.dataready.TDataNewfun;
import com.csii.adhoc.dataunits.*;
import com.csii.adhoc.dataunits.UnitBase.UnitBasePo;
import com.csii.adhoc.dataunits.UnitBase.UnitBaseVo;
import com.csii.adhoc.dataunits.addcolumn.AddColVo;
import com.csii.adhoc.dataunits.addcolumn.ColValueOrder;
import com.csii.adhoc.dataunits.addcolumn.ColumnInfo;
import com.csii.adhoc.dataunits.addcolumn.DataNewFld;
import com.csii.common.DataFromVo;
import com.github.pagehelper.PageInfo;
//import com.github.pagehelper.PageInfo;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author donglimao
 * @description 数据组件接口
 * @create 2021-01-21
 */
public interface DataUnitsService {

    List<Map<String, Object>> dataSqlQueryList(String tableId, String sql, int pageNum, int pageSize);

    List<Map<String, Object>> dataSqlQueryListCamelCase(String connId, String sql, int pageNum, int pageSize);

    List<List> dataSqlQueryListObjects(String connId, String sql, int pageNum, int pageSize);

    PageInfo dataQueryPageInfoBySqlColumn(String tableId, String[] sqlColumn, String whereCond, int pageNum, int pageSize) throws Exception;

    PageInfo dataSqlPageInfo(String tableId, String sql, int pageNum, int pageSize);

    List<Map<String, Object>> choiceTable(String tableName);

    List<TableStructure> getColumnTypes(String tableId, String type, String unitId);

    PageInfo getColumnGroupBy(String tableId, String[] sqlColumn, Integer pageNum, Integer pageSize);

    //入库组件度量信息
    void insertUnitCvg(String unitId, List<NumberSelected> numberFields);

    void deleteAllUnitKeyByUnitId(String unitId, String type);

    //入库组件分组信息
    int insertOneUnitGroup(UnitGroup unitGroup);

    /**
     * @param unitId     组件id
     * @param textFields 维度信息
     * @param type       维度类型,当前端仅有一个"维度"时,1 表示维度,当前端的"维度"分为"行维度"和"列维度时,2,表示行维度,3表示列维度
     * @descriptoin 入库组件维度信息
     */
    void insertUnitKey(String unitId, List<TextSelected> textFields, String type);

    void insertUnitCondition(Map<String, String> cond);

    /**
     * @param unitBasePo 对象
     * @description 从仪表盘新建一个组件
     */
    void insertOneUnitBase(UnitBasePo unitBasePo) throws Exception;

    /**
     * @param tableId           表 id 或 后期调整为数据集 id
     * @param textFields1       维度集合
     * @param numberFields      度量集合
     * @param condList          多层级条件表达式
     * @param defGroupSqlWhere  自定义分组信息
     * @param defSortSqlOrderBy 自定义排序信息
     * @param pageNum           页号
     * @param pageSize          每页尺寸
     * @return 返回 PageHelper 分页
     * @throws Exception
     * @description 分组表查询服务
     */
    PageInfo groupByTableQuery(String tableId, List<TextSelected> textFields1, List<NumberSelected> numberFields, List<TDataCondPo> condList, Map<String, String> defGroupSqlWhere, String filterWhereCond, Map<String, String> defSortSqlOrderBy, int pageNum, int pageSize) throws Exception;

    //获取表的表头信息
    Map<String, String> getColumnHeadInfo(String tableId) throws Exception;

    List<ColumnInfo> getColumnInfo(String tableId) throws Exception;

    List<Map<String, Object>> defaultSqlQueryList(String s);

    List<Map<String, Object>> sqlQuery2(String tableId, List<NumberSelected> numberFields, int pageNum, int pageSize, String[] sqlDeCamelText, String[] sqlDeCamelNumber, String sql);

    List<UnitBaseVo> queryUnitBaseById(String dashbordId);

    List<UnitKey> getAllUnitKey(String unitId, String type);

    void deleteAllUnitCvgByUnitId(String unitId);

    void deleteAllUnitCondition(String unitId);

    Integer getUnitType(String unitId);

    List<UnitCvg> getAllUnitCvg(String unitId);

    Map<String, String> getAllCondition(String unitId);

    void deleteAllWidgets(String unitId, String type);

    void insertAdnInfo(String unitId, String widgets, String type);

    String getAllWidgets(String unitId, String type);

    String getTableIdByModelId(String modelId);

    void updateUniteBase(UnitBasePo unitBasePo);

    String selectModelByUnitId(String unitId);

    UnitBasePo selectUnitInfoById(String unitId);

    List<AddColVo> getAllFunc(String type);

    String insertNewFld(DataNewFld dnf);

    String getFilterUnitCond(String modelId);

    List<ColumnInfo> getAllColumnByDatasetId(String datasetId) throws Exception;

    /* 纯数字字段的分段准备数据查询 对应 range 接口*/
    Map<String, Object> getIntColumnInfo(String datasetId, String fieldName);

    /* 纯文本字段的分段准备数据查询 对应 data 接口*/
    Map<String, Object> getIntColumnInfo2(String datasetId, String fieldName, Integer pageNum, Integer pageSize);

    /* 纯文本字段的模糊文字列查询 对应 search 接口*/
    Map<String, Object> getIntColumnInfo3(String datasetId, String fieldName, String keyword);

    void insertOderTable(List<ColValueOrder> selects, DataNewFld dataNewFld, String type);

    void insertTextGrp(List<TextGroup> textGroups, DataNewFld dnf);

    void insertNumberGrp(List<NumberGroup> numberGroups, DataNewFld dnf);

    /*插入*/
    void insertFunc();

    /*查询预埋的所有函数信息*/
    List<TDataNewfun> getAllTDataFunc();

    /*根据数据集id查询来源表名*/
    DataFromVo getDataFromByModelId(String tableId);

    /*函数合法性检验*/
    boolean checkFuncValid(String funcBody, String modelId);

    List<DataNewFld> getNewFieldsByModelId(String modelId);

    DataNewFld queryAddColumnById(String colId);

    void updateUniteBaseModelId(String unitId, String modelId);

    List<TDataOrd> getOrderByType(String modelId, String name, String type);

    List<TTextGrp> getTextGrpByModelId(TTextGrp record);

    List<TDataGrp> getDataGrpByModelId(NumberGroup dataGrp);

    void deleteAddedColumnById(String fieldId) throws Exception;

    List<DataNewFld> queryAllNewFldByModelId(String modelId);

    //条件树的Vo转 dto
    public static List<TDataCondPo> conTreeVo2Po(List<TDataCondVo> list, String modelId) {

        List<TDataCondPo> poList = new ArrayList<>();

        if (list == null) {
            return null;
        }

        for (TDataCondVo tDataCondVo : list) {
            TDataCondPo tDataCondPo = new TDataCondPo();
            tDataCondPo.setModelId(modelId);
            tDataCondPo.setFldEnname(tDataCondVo.getFldEnname());
            tDataCondPo.setParamType(tDataCondVo.getParamType());
            tDataCondPo.setCondOper(tDataCondVo.getCondOper());
            tDataCondPo.setValue(tDataCondVo.getValue());
            tDataCondPo.setAssist(tDataCondVo.getAssist());
            tDataCondPo.setBusiDesc(tDataCondVo.getBusiDesc());

            //递归处理子集
            List<TDataCondVo> children = tDataCondVo.getChildren();
            if (children == null) {
                tDataCondPo.setChildren(null);
            } else {
                List<TDataCondPo> childrenList = conTreeVo2Po(children, modelId);
                tDataCondPo.setChildren(childrenList);
            }
            poList.add(tDataCondPo);
        }
        return poList;
    }

    //条件树的 dto 转 Vo
    public static List<TDataCondVo> conTreePo2Vo(List<TDataCondPo> list) {
        List<TDataCondVo> poList = new ArrayList<>();

        if (list == null) {
            return null;
        }

        for (TDataCondPo tDataCondPo : list) {
            TDataCondVo tDataCondVo = new TDataCondVo();
            tDataCondVo.setFldEnname(tDataCondPo.getFldEnname());
            tDataCondVo.setParamType(tDataCondPo.getParamType());
            tDataCondVo.setCondOper(tDataCondPo.getCondOper());
            tDataCondVo.setValue(tDataCondPo.getValue());
            tDataCondVo.setAssist(tDataCondPo.getAssist());
            tDataCondVo.setBusiDesc(tDataCondPo.getBusiDesc());
            tDataCondVo.setId(tDataCondPo.getMenuId());
            tDataCondVo.setPid(tDataCondPo.getParentId());
            //递归处理子集
            List<TDataCondPo> children = tDataCondPo.getChildren();
            if (children == null) {
                tDataCondVo.setChildren(null);
            } else {
                List<TDataCondVo> childrenList = conTreePo2Vo(children);
                tDataCondVo.setChildren(childrenList);
            }
            poList.add(tDataCondVo);
        }
        return poList;
    }
}
